import actionType from './actiontype'

import {
    Login
} from '../requests'


const startLogin=()=>{
    return {
        type:actionType.STARTLOGIN
    }
}
const Loginsuccess=(ueseinfo)=>{
    return {
        type:actionType.LOGINSUCCESS,
        payload:{
            ueseinfo
        }
    }
}


const LoginFailed=()=>{
    //登录失败  清除 authToken
    window.localStorage.removeItem('authToken')
    window.sessionStorage.removeItem('authToken')
    window.localStorage.removeItem('adminInfo')
    window.sessionStorage.removeItem('adminInfo')
    return {
        type:actionType.LOGINFAILED
    }
}

//退出登录
export const Loginout=()=>{
    return (dispatch)=>{
        dispatch(LoginFailed())
    }
}
//登录
export const LoginGo=(useInfo)=>{
    return (dispatch)=>{

        dispatch(startLogin())
        Login(useInfo).then((res)=>{
            
              
            
            
                if(res.code==200){
                    dispatch(Loginsuccess(res.data)) 
                   //本地存储   记住我 与不记住我
                    if (useInfo.remember) {
                          window.localStorage.setItem('authToken',(res.data.authToken))
                          window.localStorage.setItem('adminInfo',JSON.stringify(res.data))
                    }else{
                        window.sessionStorage.setItem('authToken',res.data.authToken)
                        window.sessionStorage.setItem('adminInfo',JSON.stringify(res.data))
                    }
                   
                }else{
                    dispatch(LoginFailed())
                }
                
        })
        
    }
}